import selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from unittest import TestCase


class TestFinancing(TestCase):
    def testChangFinancing1(self):
        expect = "买入成功!"
        username = "lisi"
        password = "123456"
        pwd = "666666"
        driver = webdriver.Chrome()
        driver.maximize_window()
        driver.get("http://localhost:90")
        driver.find_element_by_xpath("//*[@id='username']").send_keys(username)
        driver.find_element_by_id('password').send_keys(password)
        driver.find_element_by_id('login_btn').click()
        time.sleep(2)
        driver.find_element_by_link_text("个人理财").click()
        time.sleep(1)
        driver.find_element_by_link_text("零钱理财").click()
        # 点击支付理财-投资
        driver.find_element_by_xpath("/html/body/div[1]/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td["
                                     "6]/button").click()
        time.sleep(3)

        # 输入支付密码
        driver.find_element_by_css_selector(".layui-layer-content > #password").send_keys(pwd)
        time.sleep(3)

        # 点击确定
        driver.find_element_by_link_text("确定").click()
        time.sleep(10)
        result = driver.find_element_by_xpath("/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[3]/div").text

        time.sleep(2)

        if expect != result:
            print("不通过")
            driver.save_screenshot(r"C:\Users\Administrator\PycharmProjects\pythonProject\TEST"
                                   r"\测试截图\零钱理财\lisi_123456_666666.png")
        else:
            print("用例通过！")
        driver.quit()
        self.assertEqual(result, expect)

    def testChangFinancing2(self):
        expect = "买入失败!"
        username = "lisi"
        password = "123456"
        pwd = "666667"
        driver = webdriver.Chrome()
        driver.maximize_window()
        driver.get("http://localhost:90")
        driver.find_element_by_xpath("//*[@id='username']").send_keys(username)
        driver.find_element_by_id('password').send_keys(password)
        driver.find_element_by_id('login_btn').click()
        time.sleep(2)
        driver.find_element_by_link_text("个人理财").click()
        time.sleep(1)
        driver.find_element_by_link_text("零钱理财").click()
        # 点击支付理财-投资
        driver.find_element_by_xpath("/html/body/div[1]/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td["
                                     "6]/button").click()
        time.sleep(3)

        # 输入支付密码
        driver.find_element_by_css_selector(".layui-layer-content > #password").send_keys(pwd)
        time.sleep(3)

        # 点击确定
        driver.find_element_by_link_text("确定").click()
        time.sleep(10)
        result = driver.find_element_by_xpath("/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[3]/div").text

        time.sleep(2)

        if expect != result:
            print("不通过")
            driver.save_screenshot(r"C:\Users\Administrator\PycharmProjects\pythonProject\TEST"
                                   r"\测试截图\零钱理财\lisi_123456_666667.png")
        else:
            print("用例通过！")
        driver.quit()
        self.assertEqual(result, expect)

    def testChangFinancing3(self):
        expect = "买入失败!，银行卡余额不足"
        username = "lisi4"
        password = "123456"
        pwd = "666666"
        driver = webdriver.Chrome()
        driver.maximize_window()
        driver.get("http://localhost:90")
        driver.find_element_by_xpath("//*[@id='username']").send_keys(username)
        driver.find_element_by_id('password').send_keys(password)
        driver.find_element_by_id('login_btn').click()
        time.sleep(2)
        driver.find_element_by_link_text("个人理财").click()
        time.sleep(1)
        driver.find_element_by_link_text("零钱理财").click()
        # 点击支付理财-投资
        driver.find_element_by_xpath("/html/body/div[1]/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td["
                                     "6]/button").click()
        time.sleep(3)

        # 输入支付密码
        driver.find_element_by_css_selector(".layui-layer-content > #password").send_keys(pwd)
        time.sleep(15)

        # 点击确定
        driver.find_element_by_link_text("确定").click()
        time.sleep(10)
        result = driver.find_element_by_xpath("/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[3]/div").text

        time.sleep(2)

        if expect != result:
            print("不通过")
            driver.save_screenshot(r"C:\Users\Administrator\PycharmProjects\pythonProject\TEST"
                                   r"\测试截图\零钱理财\lisi4_123456_666666.png")
        else:
            print("用例通过！")
        driver.quit()
        self.assertEqual(result, expect)

    def testChangFinancing4(self):
        expect = "买入失败!，请绑定银行卡"
        username = "lisi5"
        password = "123456"
        pwd = "666666"
        driver = webdriver.Chrome()
        driver.maximize_window()
        driver.get("http://localhost:90")
        driver.find_element_by_xpath("//*[@id='username']").send_keys(username)
        driver.find_element_by_id('password').send_keys(password)
        driver.find_element_by_id('login_btn').click()
        time.sleep(2)
        driver.find_element_by_link_text("个人理财").click()
        time.sleep(1)
        driver.find_element_by_link_text("零钱理财").click()
        # 点击支付理财-投资
        driver.find_element_by_xpath("/html/body/div[1]/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td["
                                     "6]/button").click()
        time.sleep(3)

        # 输入支付密码
        driver.find_element_by_css_selector(".layui-layer-content > #password").send_keys(pwd)
        time.sleep(3)

        # 点击确定
        driver.find_element_by_link_text("确定").click()
        time.sleep(10)
        result = driver.find_element_by_xpath("/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[3]/div").text

        time.sleep(2)

        if expect != result:
            print("不通过")
            driver.save_screenshot(r"C:\Users\Administrator\PycharmProjects\pythonProject\TEST"
                                   r"\测试截图\零钱理财\lisi5_123456_666666.png")
        else:
            print("用例通过！")
        driver.quit()
        self.assertEqual(result, expect)